<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <jsp:include page="/WEB-INF/views/admin/common/import.jsp">
        <jsp:param name="title" value="商品管理"/>
    </jsp:include>

</head>
<body>

<div class="grid-container">
    <jsp:include page="/WEB-INF/views/admin/common/navbar.jsp">
        <jsp:param name="active" value="product"/>
    </jsp:include>

    <jsp:include page="/WEB-INF/views/admin/common/header.jsp">
        <jsp:param name="title" value="商品管理-新增"/>
    </jsp:include>
    <main class="p-4">
        <form class="row g-3" action="${pageContext.request.contextPath}/admin/product?method=create" method="post"
              enctype="multipart/form-data">

            <div class="form-group">
                <label for="name" class="form-label">商品名称*</label>
                <input type="text" class="form-control" id="name" name="name" required
                       oninvalid="setCustomValidity('请输入商品名称！');" oninput="setCustomValidity('');">
            </div>


            <div class="form-group">
                <label for="categoryId" class="form-label">分类*</label>
                <select class="form-control" id="categoryId" name="categoryId" required
                        oninvalid="setCustomValidity('请选择分类！');" oninput="setCustomValidity('');">
                    <option value="">-- 请选择分类 --</option>
                    <c:forEach items="${categoryList}" var="category">
                        <option value="${category.id}">${category.name}</option>
                    </c:forEach>
                </select>
            </div>
            <div class="form-group">
                <label for="price" class="form-label">价格*</label>
                <input type="number" step="0.01" min="0" class="form-control" id="price" name="price"
                       required
                       oninvalid="setCustomValidity('请输入有效价格！');" oninput="setCustomValidity('');">
            </div>


            <div class="form-group">
                <label for="description" class="form-label">描述</label>
                <textarea class="form-control" id="description" name="description" rows="5"></textarea>
            </div>

            <!-- 封面图片部分 -->
            <div class="form-group">
                <label for="image" class="form-label">图片*</label>
                <input type="file" class="form-control" id="image" name="image" accept="image/*" required
                       oninvalid="setCustomValidity('请上传商品图片！');" oninput="setCustomValidity('');">
                <small class="form-text text-muted">建议尺寸: 300x400像素，支持JPG/PNG格式</small>
            </div>

            <!-- 操作按钮 -->
            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-submit">提交</button>
                <a href="${pageContext.request.contextPath}/admin/product?method=viewList"
                   class="btn btn-secondary btn-submit">返回列表</a>
            </div>
        </form>
    </main>
</div>

<script>
  // 设置默认日期为今天
  document.addEventListener('DOMContentLoaded', function () {
    const today = new Date();
    const year = today.getFullYear();
    const month = String(today.getMonth() + 1).padStart(2, '0');
    const day = String(today.getDate()).padStart(2, '0');
    document.getElementById('publishDate').value = `${year}-${month}-${day}`;

    // 设置日期选择范围（可选）
    const dateInput = document.getElementById('publishDate');
    dateInput.max = `${year}-${month}-${day}`; // 不能选择今天之后的日期
    dateInput.min = "1900-01-01"; // 最早可选日期
  });
</script>
</body>
</html>
